package org.example.musicserver.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.musicserver.common.Result;
import org.example.musicserver.entity.MV;
import org.example.musicserver.mapper.MVMapper;
import org.example.musicserver.service.MVService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Service
@Slf4j
public class MVServiceImpl extends ServiceImpl<MVMapper, MV> implements MVService {
    @Resource
    private MVMapper mvMapper;

    @Override
    public Result addMV(MV mv) {
        if (mvMapper.insert(mv) > 0) {
            return Result.success("添加成功", true);
        } else {
            return Result.error("添加失败");
        }
    }

    @Override
    public Result deleteMV(Integer mvId) {
        if (mvMapper.deleteById(mvId) > 0) {
            return Result.success("添加成功", true);
        } else {
            return Result.error("添加失败");
        }
    }

    @Override
    public Result updateMV(MV mv) {
        if (mvMapper.updateById(mv) > 0) {
            return Result.success("添加成功", true);
        } else {
            return Result.error("添加失败");
        }
    }

    @Override
    public Result selectMV(String query,Integer singId) {

        QueryWrapper<MV> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",query).orderByDesc("create_time");
        if(singId != null){
            queryWrapper.eq("singId",singId);
        }
        List<MV> mvs = mvMapper.selectList(queryWrapper);
        return Result.success("返回结果成功",mvs);
    }

    @Override
    public Result uploadMV(MultipartFile file,String type) {
        String[] split = file.getOriginalFilename().split("\\.");
        String s = split[split.length - 1];
        String path = "img/" + type + "/" + System.currentTimeMillis() + "." +  s;
        File newFile = new File(path);
        try {
            if(!newFile.getParentFile().exists()){
                newFile.getParentFile().mkdir();
            }
            if(!newFile.exists()){
                newFile.createNewFile();
            }

            file.transferTo(newFile.toPath());
        } catch (IOException e) {
            log.warn("操作失败{}",e);
            return Result.error("添加失败");
        }

        return Result.success("上传成功","/" + path);
    }

    @Override
    public Result detail(Integer id) {
        MV mv = mvMapper.selectById(id);
        return Result.success("返回结果成功",mv);
    }

    @Override
    public Result selectByMvIds(List<Integer> ids) {
        QueryWrapper<MV> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("mv_id",ids).orderByDesc("create_time");
        List<MV> mvs = mvMapper.selectList(queryWrapper);
        return Result.success("返回结果成功",mvs);
    }
}
